package com.hns.can.base.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hns.can.base.entity.SysAnnouncementEntity;
import com.hns.can.base.entity.SysAnnouncementReadEntity;
import com.hns.can.base.vo.SystemAnnounceVo;
import com.hns.frame.mybatis.Pagination;
import com.hns.frame.service.CommonService;
import com.hns.tool.pub.PubUtil;

/**
 * 系统公告service
 * @author:zhangyu
 * @email:zhangyu2222z@sina.cn
 * @version Revision 2.0.0
 */
@Service
public class SysSystemAnnounceService extends CommonService {

	/**
	 * 查询系统公告
	 * @param title 标题
	 * @param corpId 公司id
	 * @return 系统公告列表
	 * @author:zhangyu
	 * @email:zhangyu2222z@sina.cn
	 */
	public Pagination getSystemAnnounce(String title, String corpId) {
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("title", title);
		map.put("corpId", corpId);
		Pagination page = this.myBatisPage("systemAnnounce.getSystemAnnounceCount", "systemAnnounce.getSystemAnnounceInfo", map, SystemAnnounceVo.class);
		return page;
	}

	/**
	 * 添加系统公告
	 * @param entity 系统公告信息
	 * @author:zhangyu
	 * @email:zhangyu2222z@sina.cn
	 */
	@Transactional
	public void addSystemAnnounce(SysAnnouncementEntity entity) {
		this.commonEntityDao.save(entity);
	}

	/**
	 * 删除系统公告
	 * @param docIds 公告id
	 * @author:zhangyu
	 * @email:zhangyu2222z@sina.cn
	 */
	@Transactional
	public void deleteSystemAnnounce(String[] docIds) {
		commonEntityDao.delete(SysAnnouncementEntity.class, docIds);
	}

	/**
	 * 获取指定公告
	 * @param announId 公告id
	 * @return 公告实体
	 * @author:zhangyu
	 * @email:zhangyu2222z@sina.cn
	 */
	public SysAnnouncementEntity getAnnounceContent(String announId) {
		SysAnnouncementEntity announce = this.commonEntityDao.find(announId, SysAnnouncementEntity.class);
		return announce;
	}

	/**
	 * 保存已读信息
	 * @param entity 公告已读状态实体
	 * @author:zhangyu
	 * @email:zhangyu2222z@sina.cn
	 */
	@Transactional
	public void saveReadAnnounce(SysAnnouncementReadEntity entity) {
		Map map = new HashMap();
		map.put("announId", entity.getAnnounId());
		List isRead = this.commonEntityDao.findByParamsIgnoreNullValue(SysAnnouncementReadEntity.class, map);
		if(PubUtil.isEmpty(isRead)){
			this.commonEntityDao.save(entity);
		}
	}

}
